class EventsController < ApplicationController
  # GET /events
  # GET /events.xml
  def index
    @events = Event.all
    @channels =Channel.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @events }
    end
  end

  # GET /events/1
  # GET /events/1.xml
  def show
    @event = Event.find(params[:id])
    @channel= Channel.all
    @articles=Article.all
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @event }
    end
  end

  # GET /events/new
  # GET /events/new.xml
  def new
    @event = Event.new
    @channels =Channel.all

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @event }
    end
  end

  # GET /events/1/edit
  def edit
    @events = Event.find(params[:id])
    @channels =@events.channel_id
    @idevent=@events.id
    @images=Image.where("event_id='#{@idevent}'")
  end

  # POST /events
  # POST /events.xml
  def create
    @event = Event.new(params[:event])    
    respond_to do |format|
      if @event.save
        @id=@event.id
        #format.html { redirect_to(@event, :notice => 'Event was successfully created.') }
        #format.xml  { render :xml => @event, :status => :created, :location => @event }
         format.html { redirect_to(:action => "edit", :id =>@id)}
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /events/1
  # PUT /events/1.xml
  def update
    @event = Event.find(params[:id])
    #@channels =Channel.find(params[:id]

    respond_to do |format|
      if @event.update_attributes(params[:event])
        format.html { redirect_to(@event, :notice => 'Event was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /events/1
  # DELETE /events/1.xml
  def destroy
    @event = Event.find(params[:id])
    @event.destroy

    respond_to do |format|
      format.html { redirect_to(events_url) }
      format.xml  { head :ok }
    end
  end
  
  def createnew
    @event = Event.create(params[:event])
    @channels=Channel.all
    #@bien = params[:txtImagetitle]
    @name=params[:upload]['datafile'].original_filename
    @id=@event.id
    @name=@id.to_s+"_" + @name
    @path = "/images/events/"+@name
    @discription=params[:txtDiscription]
    #respond_to do |format|
      #if @event.save
        #format.html { redirect_to(@event, :notice => 'Event was successfully created.') }
        #format.xml  { render :xml => @event, :status => :created, :location => @event }
        @event.images.create(:title => "#{@name}" ,:path => "#{@path}",:description=>"#{@discription}")
        #format.html { redirect_to(@event, :notice => @name)}
      #else
         render :action => "new"
        #format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      #end
    #end
  end
  
  def uploadfile
    #Lay id cua su kien     
     @events = Event.find(params[:idevents])
     @idevent=@events.id
    
    @name = params[:upload]['datafile'].original_filename
    directory = "public/images/events"
    #tao duong dan luu
    @path=File.join(directory,@name)
    #ghi file
    File.open(@path,"wb") {|f| f.write(params[:upload]['datafile'].read) } 
    @events.images.create(:title=>"#{@name}",:path=>"#{@path}")
    @image=Image.find_by_event_id(@idevent)
    @title=@image.title
    respond_to do |format|
      format.html { redirect_to(:action => "edit", :id =>@idevent)}
    end
  end
  
  def up_another_imges
    @articles=Article.all
    @events=Event.all
    @channels=Channel.all
    render :file => 'app/views/events/up_another_images.html.erb'
  end
  
  def editevent
    @events = Event.find(params[:id])
    #@channels =@events.channel_id
   # @idevent=@events.id
    #@image=Image.find_by_event_id(@idevent)
    #@title=@image.title
    #if @title ==nil
     # @title=" ban"
    #end
  end
end

